import axios from 'axios'
//"proxy": "http://127.0.0.1:3030",
// 代理配置
const proxySwitch = () => {
	return (process.env.NODE_ENV !== 'production')
}
// 全局请求头设置
axios.defaults.headers = {
	'Content-Type': 'application/json',
	'token': 'zqf666',
}
//统一设置请求前缀
axios.defaults.baseURL = "http://127.0.0.1:3030"

// http request 拦截器
axios.interceptors.request.use(function(config) {
	config.url = '/todo_api' + config.url
	return  config
}, err => {
	if (err && err.config) {
		switch (err.config.status) {
		case 400: err.message = '请求错误(400)'; break
		case 401: err.message = '未授权，请重新登录(401)'; break
		case 403: err.message = '拒绝访问(403)'; break
		case 404: err.message = '请求出错(404)'; break
		case 408: err.message = '请求超时(408)'; break
		case 500: err.message = '服务器错误(500)'; break
		case 501: err.message = '服务未实现(501)'; break
		case 502: err.message = '网络错误(502)'; break
		case 503: err.message = '服务不可用(503)'; break
		case 504: err.message = '网络超时(504)'; break
		case 505: err.message = 'HTTP版本不受支持(505)'; break
		default: err.message = `连接出错(${err.config.status})!`
		}
	} else {
		err.message = '连接服务器失败!'
	}
	return Promise.reject(err)
})

// http response 拦截器
axios.interceptors.response.use(function(response) {
	return response
}, err => {
	return Promise.reject(err)
})

// 封装post请求
export const _post = (url, data) => {
    return new Promise((resolve, reject) => {
        axios.post(url, data).then(response => {
			resolve(response.data)
		}).catch(err => {
			reject(err)
		})
    })
}

// 封装get请求
export const _get = (url, data) => {
    return new Promise((resolve, reject) => {
        axios.get(url, {params: data}).then(response => {
			resolve(response.data)
		}).catch(err => {
			reject(err)
		})
    })
}

